
package com.updesignsz.admin.controller;

import com.updesignsz.admin.service.ProjectsService;
import com.updesignsz.admin.util.ResponseUtil;
import com.updesignsz.admin.vo.form.ProjectsForm;
import com.updesignsz.commom.entity.News;
import com.updesignsz.commom.entity.Projects;
import com.updesignsz.commom.util.DateUtil;
import com.updesignsz.commom.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 项目
 * Creation date 2020-8-8 15:07:53
 */
@RestController
public class ProjectsController {

	@Autowired
	private ProjectsService projectsService;
	/**
	 * 项目类型列表
	 */
	@GetMapping("/projects")
	public void insuranceProjects(Projects projects, HttpServletResponse response) {
		ResultVo vo = new ResultVo();
		vo.setData(projectsService.listAll(projects));
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}

	/**
	 * 保存 项目类型
	 */
	@PostMapping("/projects")
	public void addProjects(@Validated(PostMapping.class) @RequestBody ProjectsForm form, HttpServletResponse response) {
		ResultVo vo = new ResultVo();
		Projects projects = convert(form);
		projectsService.saveProjects(projects);
		vo.setData(projectsService.getById(projects.getId()));
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}


	/**
	 * 删除 项目类型
	 */
	@DeleteMapping("/projects/{projectsId}")
	public void deleteProjects(@PathVariable Integer projectsId, HttpServletResponse response) {
		ResultVo vo = new ResultVo();
		projectsService.deleteProjects(projectsId);
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}

	/**
	 * 修改 项目类型
	 */
	@PutMapping("/projects")
	public void updateProjects(@Validated(PutMapping.class) @RequestBody ProjectsForm form, HttpServletResponse response) {
		ResultVo vo = new ResultVo();
		Projects projects = convert(form);
		projectsService.updateProjects(projects);
		vo.setData(projectsService.getById(projects.getId()));
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}

	/**
	 * 修改 项目
	 */
	@PatchMapping("/projects")
	public void updateNews(@Validated(PatchMapping.class) @RequestBody Projects news, HttpServletResponse response) {
		ResultVo vo = new ResultVo();
		projectsService.updateProjects(news);
		vo.setData(projectsService.getById(news.getId()));
		ResponseUtil.outJson(vo.toJsonMsg(), response);
	}

	private Projects convert(ProjectsForm projectsForm) {
		Projects projects = new Projects();
		projects.setId(projectsForm.getId());
		projects.setName(projectsForm.getName());
		projects.setArea(projectsForm.getArea());
		projects.setViewDesign(projectsForm.getViewDesign());
		projects.setOwnerName(projectsForm.getOwnerName());
		projects.setType(projectsForm.getType());
		projects.setArchitecture(projectsForm.getArchitecture());
		projects.setCompleteDate(DateUtil.getStrByDataTime(projectsForm.getCompleteDate(), "yyyy-MM-dd"));
		String[] images = projectsForm.getProjectImage().split(",");
		if (images.length > 0) {
			projects.setCover(images[0]);
			projects.setCoverThumbnail(images[0].replace("uploads/", "uploads/thumb_"));
			projects.setProjectImage(projectsForm.getProjectImage().substring(0, projectsForm.getProjectImage().length() - 1));
		}
		return projects;
	}
}